
/*
 * title:js_open_budget_do.js
 * date:2012-4-6
 * author:taojiang
 * copyright:qingneng
 */

$(document).ready(function(){

	
var products = [
    		    {b_item:'市区',value:'市区-0'},
    		    {b_item:'郊区',value:'郊区-300'},
    		    {b_item:'无隔油池',value:'无隔油池-0'},
    		    {b_item:'有隔油池',value:'有隔油池-150'},
    		    {b_item:'施工环境优',value:'施工环境优-0'},
    		    {b_item:'施工环境差',value:'施工环境差-100'},
    		    {b_item:'化粪池使用年限低于5年',value:'化粪池使用年限低于5年-0'},
    		    {b_item:'化粪池使用年限超过5年',value:'化粪池使用年限超过5年-200'},
    		    {b_item:'水电费甲供',value:'水电费甲供-0'},
    		    {b_item:'水电费乙供',value:'水电费乙供-0.04*c'}
    		];

function productFormatter(value){
	for(var i=0; i<products.length; i++){
		if (products[i].value == value) return products[i].b_item;
	}
	return value;
}



//初始化工程量表
$(function(){
	//异步获取信息
	//使用异步程序，在后台进行计算	
	var params = {
			project_workinfo_id:project_workinfo_id
			};
	var url = baseURL+"/workQuantitiesAction!loadQuantities.do";
	
	jQuery.post(url,params,function(data){
			if(data!=''){
				//工程量表页面中
				$('#wasteWaterCapacity').attr('value',data.wasteWaterCapacity);
				$('#sludgeCapacity').attr('value',data.sludgeCapacity);
				$('#effectiveCapacity').attr('value',data.effectiveCapacity);	
				
				
			}
		
	},'json');
		
});


//初始化工程量建筑类型项
var lastIndex = -1;
$(function(){
	$('#dg—gcl').datagrid({
		queryParams:{
			project_workinfo_id:project_workinfo_id
		},
		url:baseURL+'/loadQuantitesItemAction.do',
		/*toolbar:[{
			text:'计算',
			iconCls:'icon-ok',
			handler:function(){
				
				$('#dg—gcl').datagrid('endEdit',lastIndex);
				_cal_quantities_do();
			}
		}],*/
		onClickRow:function(rowIndex){
			if (lastIndex != rowIndex){
				$('#dg—gcl').datagrid('endEdit', lastIndex);
				$('#dg—gcl').datagrid('beginEdit', rowIndex);
			}
			lastIndex = rowIndex;
		}
		
		
	});
	
});



//初始化取费表
$(function(){
	$('#dg—qfb').datagrid({
		queryParams:{
			project_workinfo_id:project_workinfo_id
		},
		url:baseURL+'/loadFeeItemAction.do',
		rowStyler:function(index,row){
			if (index<3){
				return 'background-color:pink;color:blue;font-weight:bold;';
			}
			if (index==6){
				return 'background-color:red;color:blue;font-weight:bold;';
			}
		}
		/*toolbar:[{
			text:'计算',
			iconCls:'icon-ok',
			handler:function(){
				_cal_fee_do();
			}
	
		}]
		*/
		
	});
	
});


/*var gdst_data = [];
//初始化管道疏通
$(function(){

	var parent_item_id = 4;
		
	var params = {
			parent_item_id:parent_item_id
			};
	var url = baseURL+"/loadBudgetItem_gdstAction!loadSubBudgetItem.do";
	
	jQuery.post(url,params,function(data){
			if(data!=''){
				gdst_data = data.rows;
				//alert(gdst_data[0].budget_item_value);
			}
		
	},'json');
	
});*/

var hfc_qt_data = [];
//初始化化粪池清掏
$(function(){

	var parent_item_id = 1;
		
	var params = {
			parent_item_id:parent_item_id
			};
	var url = baseURL+"/loadBudgetItem_gdstAction!loadSubBudgetItem.do";
	
	jQuery.post(url,params,function(data){
			if(data!=''){
				hfc_qt_data = data.rows;
				//alert(gdst_data[0].budget_item_value);
			}
		
	},'json');
	
});


var hfcwh_date_data = [];
//初始化化粪池维护按时间
$(function(){

	var parent_item_id = 6;
		
	var params = {
			parent_item_id:parent_item_id
			};
	var url = baseURL+"/loadBudgetItem_gdstAction!loadSubBudgetItem.do";
	
	jQuery.post(url,params,function(data){
			if(data!=''){
				hfcwh_date_data = data.rows;
				//alert(gdst_data[0].budget_item_value);
			}
		
	},'json');
	
});

var hfcwh_times_data = [];
//初始化化粪池维护按次数
$(function(){

	var parent_item_id = 7;
		
	var params = {
			parent_item_id:parent_item_id
			};
	var url = baseURL+"/loadBudgetItem_gdstAction!loadSubBudgetItem.do";
	
	jQuery.post(url,params,function(data){
			if(data!=''){
				hfcwh_times_data = data.rows;
				//alert(gdst_data[0].budget_item_value);
			}
		
	},'json');
	
});



/*//管道疏通计算结果
function gdst_save(rows){
	
	//使用异步程序，在后台进行计算
	if(rows!=''){
		var arrayParam ='';
		for(i=0;i<rows.length;i++){
			if(rows[i].budget_item!=''){
				rows[i].budget_item_name = rows[i].budget_item;
			}
			var json2String = O2String(rows[i]);
			//arrayParam.push(json2String);
			arrayParam = arrayParam+json2String+"|";
		}
		//alert(arrayParam);
		var parent_item_id = 4;
		
		var params = {rowlist:arrayParam,project_workinfo_id:project_workinfo_id,parent_item_id:parent_item_id} ;
		var url = baseURL+"/loadBudgetItem_gdstAction!saveSubBudgetItem.do";
		
		jQuery.post(url,params,function(data){
				if(data!=''){
					//gdst_data = data.rows;
					//alert(gdst_data[0].budget_item_value);
				}
			
		},'json');
	
	}
		
}*/

//化粪池清掏结果
function hfc_qt_save(rows){
	
	//使用异步程序，在后台进行计算
	if(rows!=''){
		var arrayParam ='';
		for(i=0;i<rows.length;i++){
			if(rows[i].budget_item!=''){
				rows[i].budget_item_name = rows[i].budget_item;
			}
			var json2String = O2String(rows[i]);
			//arrayParam.push(json2String);
			arrayParam = arrayParam+json2String+"|";
		}
		//alert(arrayParam);
		var parent_item_id = 1;
		
		var params = {rowlist:arrayParam,project_workinfo_id:project_workinfo_id,parent_item_id:parent_item_id} ;
		var url = baseURL+"/loadBudgetItem_gdstAction!saveSubBudgetItem.do";
		
		jQuery.post(url,params,function(data){
				if(data!=''){
					//gdst_data = data.rows;
					//alert(gdst_data[0].budget_item_value);
				}
			
		},'json');
	
	}
		
}


//化粪池维护按时间段保存
function hfcwh_date_save(rows){
	
	//使用异步程序，在后台进行计算
	if(rows!=''){
		var arrayParam ='';
		for(i=0;i<rows.length;i++){
			if(rows[i].budget_item!=''){
				rows[i].budget_item_name = rows[i].budget_item;
			}
			var json2String = O2String(rows[i]);
			//arrayParam.push(json2String);
			arrayParam = arrayParam+json2String+"|";
		}
		//alert(arrayParam);
		
		var parent_item_id = 6;
		
		var params = {rowlist:arrayParam,project_workinfo_id:project_workinfo_id,parent_item_id:parent_item_id} ;
		var url = baseURL+"/loadBudgetItem_gdstAction!saveSubBudgetItem.do";
		
		jQuery.post(url,params,function(data){
				if(data!=''){
					//alert(data.rows);
					//hfcwh_date_data = data.rows;
					//alert(gdst_data[0].budget_item_value);
				}
			
		},'json');
	
	}
		
}

//化粪池维护按维护次数段保存
function hfcwh_times_save(rows){
	
	//使用异步程序，在后台进行计算
	if(rows!=''){
		var arrayParam ='';
		for(i=0;i<rows.length;i++){
			if(rows[i].budget_item!=''){
				rows[i].budget_item_name = rows[i].budget_item;
			}
			var json2String = O2String(rows[i]);
			//arrayParam.push(json2String);
			arrayParam = arrayParam+json2String+"|";
		}
		//alert(arrayParam);
		var parent_item_id = 7;
		var params = {rowlist:arrayParam,project_workinfo_id:project_workinfo_id,parent_item_id:parent_item_id} ;
		var url = baseURL+"/loadBudgetItem_gdstAction!saveSubBudgetItem.do";
		
		jQuery.post(url,params,function(data){
				if(data!=''){
					//hfcwh_times_data = data.rows;
					//alert(gdst_data[0].budget_item_value);
				}
			
		},'json');
	
	}
		
}





//初始化预算表
$(function(){
	$('#dg').datagrid({
		view: detailview,
		url :baseURL+'/loadBudgetItemAction.do',
		queryParams:{
			project_workinfo_id:project_workinfo_id
		},
		rowStyler:function(index,row){
			if (row.totalAmount==0){
				return 'background-color:pink;color:blue;font-weight:bold;';
			}
		},
		detailFormatter:function(index,row){
			
			return '<div style="padding:2px"><table id="ddv-' + index + '"></table></div>';
			
		},
		/*toolbar:[{
			text:'汇总',
			iconCls:'icon-ok',
			handler:function(){
				_cal_budget_do();
				
			}
	
		}],*/
		onExpandRow: function(index,row){
					
			/*if(index==0){
				
				 //var selected = $('#dg').datagrid('getSelected');
				 var parent_item_id = 1;
							
				$('#ddv-'+index).datagrid({
					url:baseURL+'/loadBudgetItem_hfc_clAction.do',
					queryParams:{
						parent_item_id:parent_item_id,
						project_workinfo_id:project_workinfo_id,
						wasteWaterCapacity:$('#wasteWaterCapacity').val(),
						effectiveCapacity:$('#effectiveCapacity').val(),
						sludgeCapacity:$('#sludgeCapacity').val()
						},		
					fitColumns:true,
					singleSelect:true,
					height:'auto',
					columns:[[
						{field:'budget_item_value',title:'名称',width:210},
						{field:'item_unit',title:'单位',width:100},
						{field:'budget_do_quantities',title:'工程量',width:100,editor:'numberbox'},
						{field:'item_unit_price',title:'单价',width:100},
						{field:'totalAmount',title:'合价',width:100}
					
					]],
					toolbar:[{
						text:'保存',
						iconCls:'icon-save',
						handler:function(){
	
							var rows = $('#ddv-'+index).datagrid('getRows');
							var total =0;
							for(i=0;i<rows.length;i++){
								var tmp = rows[i].totalAmount;
								//alert(tmp);
								if(!isNaN(tmp)){
									total = total+parseFloat(tmp);
								}
							}
							//alert(totalPrice);
							
							$('#dg').datagrid('updateRow',{index:index,row:{totalAmount:total}});
							
						}
				
					}],
					  onBeforeEdit:function(subindex,subrow){ 	
						 subrow.editing = true;   
						 $('#ddv-'+index).datagrid('refreshRow',subindex);   
						},  
						  onAfterEdit:function(subindex,subrow){   
							  subrow.editing = false;   
							  subrow.c_total = subrow.c_work_gzl*subrow.c_dj;
							  $('#ddv-'+index).datagrid('refreshRow',subindex);   
						 },     
					   onClickRow:function(rowIndex){
							if (lastIndex != rowIndex){
								$('#ddv-'+index).datagrid('endEdit', lastIndex);
								$('#ddv-'+index).datagrid('beginEdit', rowIndex);
							}
							lastIndex = rowIndex;
						},
						 
					onResize:function(){
						$('#dg').datagrid('fixDetailRowHeight',index);
					},
					onLoadSuccess:function(){
						setTimeout(function(){
							$('#dg').datagrid('fixDetailRowHeight',index);
						},0);
					}
				});
			};*/
			
			if(index==0){
				
				var parent_item_id = 1;
				
				var v_budget_num = $("#budget_num").val();
				var v_budget_avgCapacity = $("#budget_avgCapacity").val();
								 
				 $('#ddv-'+index).datagrid({
						url:baseURL+'/loadBudgetItem_gdstAction!loadhfcQtBudgetDoneItem.do',
						queryParams:{
							parent_item_id:parent_item_id,
							project_workinfo_id:project_workinfo_id
							},		
						fitColumns:true,
						singleSelect:true,
						height:'auto',
						columns:[[
							{field:'budget_item_name',title:'名称',width:210,
								formatter:function(value,rowData,rowIndex){
									for(var i=0; i<hfc_qt_data.length; i++){
										if (hfc_qt_data[i].budget_item_name == value) return hfc_qt_data[i].budget_item_value;
									}
									return value;
								},
	
								 editor:{   
						                type:'combobox',   
						                options:{   
						                valueField:'budget_item_name',   
						                textField:'budget_item_value',   
						                data:hfc_qt_data,   
						                required:true ,
						                onSelect:function(record){
						                	//alert(record.value);
						                	var totalAmount=0.0;
						                	var formla = record.item_formula;
						                	//平均容积≤150
						                	if(record.budget_item_name!='hfc_cl_qt_1'){
							                	var unit_price = record.item_unit_price;
							                	totalAmount = unit_price*v_budget_avgCapacity*v_budget_num;
						                	}else{
						                		//3700*(A/2)+500*(A/2);
						                		var reg=new RegExp("A","g"); //创建正则RegExp对象   
						                		//alert(formla);
						                		var f = formla.replace(reg,v_budget_num);
						                		totalAmount = eval(f);
						                	}
						                	//alert(record.item_id);
						                	//alert(record.budget_item_name);
						                	var row = $('#ddv-'+index).datagrid('getSelected');
						                	var rowindex = $('#ddv-'+index).datagrid('getRowIndex', row);
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{item_id:record.item_id}});
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{budget_item:record.budget_item_name}}); 
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{budget_item_value:record.budget_item_value}}); 
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{item_unit_price:record.item_unit_price}}); 
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{totalAmount:totalAmount}}); 
						                } 
						                 }   
						           }   
							
							
							
							},
							{field:'item_id',title:'id',width:10,hidden:true},
							{field:'budget_item',title:'budget_item',hidden:true},
							{field:'budget_item_value',title:'budget_item_value',width:10,hidden:true},
							{field:'item_unit',title:'单位',width:100},
							{field:'budget_do_quantities',title:'工程量',width:100,editor:'numberbox'},
							{field:'item_unit_price',title:'单价',width:100},
							{field:'totalAmount',title:'合价',width:100}
						
						]],
						toolbar:[{
							text:'确定',
							iconCls:'icon-save',
							handler:function(){
		
								var rows = $('#ddv-'+index).datagrid('getRows');
								var total =0.0;
								var t_budget_do_quantities =0.0;
								for(i=0;i<rows.length;i++){
									var tmp = rows[i].totalAmount;
									var tmpq = rows[i].budget_do_quantities;
									//alert(tmp);
									if(!isNaN(tmp)){
										total = total+parseFloat(tmp);
									}
									if(!isNaN(tmpq)){
										t_budget_do_quantities = t_budget_do_quantities+parseFloat(tmpq);
									}
								}
								//alert(totalPrice);
								$('#dg').datagrid('updateRow',{index:index,row:{totalAmount:total}});
								$('#dg').datagrid('updateRow',{index:index,row:{budget_do_quantities:t_budget_do_quantities}});
								
								//保存到后台
								hfc_qt_save(rows);
								
							}
					
						}],
						      
					    onClickRow:function(rowIndex){
								if (lastIndex != rowIndex){
									$('#ddv-'+index).datagrid('endEdit', lastIndex);
									$('#ddv-'+index).datagrid('beginEdit', rowIndex);
								}
								lastIndex = rowIndex;
							},
							 
						onResize:function(){
							$('#dg').datagrid('fixDetailRowHeight',index);
						},
						onLoadSuccess:function(){
							setTimeout(function(){
								$('#dg').datagrid('fixDetailRowHeight',index);
							},0);
						}
					});
			};
			
			
			
			/*// 管道疏通

			if(index==3){
				
				var parent_item_id = 4;
				
				// var selected = $('#dg').datagrid('getSelected');
				// var parent_item_id = selected.item_id;
				 
				 $('#ddv-'+index).datagrid({
						url:baseURL+'/loadBudgetItem_gdstAction!loadgdstBudgetDoneItem.do',
						queryParams:{
							parent_item_id:parent_item_id,
							project_workinfo_id:project_workinfo_id
							},		
						fitColumns:true,
						singleSelect:true,
						height:'auto',
						columns:[[
							{field:'budget_item_name',title:'名称',width:210,
								formatter:function(value,rowData,rowIndex){
									for(var i=0; i<gdst_data.length; i++){
										if (gdst_data[i].budget_item_name == value) return gdst_data[i].budget_item_value;
									}
									return value;
								},
	
								 editor:{   
						                type:'combobox',   
						                options:{   
						                valueField:'budget_item_name',   
						                textField:'budget_item_value',   
						                data:gdst_data,   
						                required:true ,
						                onSelect:function(record){
						                	//alert(record.value);
						                	var totalAmount=0.0;
						                	//获得公式
						                	if(record.budget_item_name=="gdst_sub_greate50"){
						                		totalAmount = 10000;
						                	}else{
						                		totalAmount = record.item_unit_price;
						                	}
						                	//alert(record.item_id);
						                	//alert(record.budget_item_name);
						                	var row = $('#ddv-'+index).datagrid('getSelected');
						                	var rowindex = $('#ddv-'+index).datagrid('getRowIndex', row);
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{item_id:record.item_id}});
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{budget_item:record.budget_item_name}}); 
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{budget_item_value:record.budget_item_value}}); 
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{item_unit_price:record.item_unit_price}}); 
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{totalAmount:totalAmount}}); 
						                } 
						                 }   
						           }   
							
							
							
							},
							{field:'item_id',title:'id',width:10,hidden:true},
							{field:'budget_item',title:'budget_item',hidden:true},
							{field:'budget_item_value',title:'budget_item_value',width:10,hidden:true},
							{field:'item_unit',title:'单位',width:100},
							{field:'budget_do_quantities',title:'工程量',width:100,editor:'numberbox'},
							{field:'item_unit_price',title:'单价',width:100},
							{field:'totalAmount',title:'合价',width:100}
						
						]],
						toolbar:[{
							text:'保存',
							iconCls:'icon-save',
							handler:function(){
		
								var rows = $('#ddv-'+index).datagrid('getRows');
								var total =0;
								for(i=0;i<rows.length;i++){
									var tmp = rows[i].totalAmount;
									//alert(tmp);
									if(!isNaN(tmp)){
										total = total+parseFloat(tmp);
									}
								}
								//alert(totalPrice);
								$('#dg').datagrid('updateRow',{index:index,row:{totalAmount:total}});
								
								//保存到后台
								gdst_save(rows);
								
							}
					
						}],
						      
					    onClickRow:function(rowIndex){
								if (lastIndex != rowIndex){
									$('#ddv-'+index).datagrid('endEdit', lastIndex);
									$('#ddv-'+index).datagrid('beginEdit', rowIndex);
								}
								lastIndex = rowIndex;
							},
							 
						onResize:function(){
							$('#dg').datagrid('fixDetailRowHeight',index);
						},
						onLoadSuccess:function(){
							setTimeout(function(){
								$('#dg').datagrid('fixDetailRowHeight',index);
							},0);
						}
					});
			};
			
			// 管道清洗

			if(index==4){
				//<option>无管道清洗低于50m</option>
				//<option>无管道清洗超过50</option>
				
				var gdqx_data = [{item:'无管道清洗',value:'0'},
				                 {item:'无管道清洗低于50m',value:'1500'},
				                 {item:'无管道清洗超过50',value:'25'}];
				 var selected = $('#dg').datagrid('getSelected');
				 var parent_item_id = selected.id;
				 
				 $('#ddv-'+index).datagrid({
						url:'gdst.json',
						queryParams:{
							parent_item_id:parent_item_id,
							project_workinfo_id:project_workinfo_id
							},		
						fitColumns:true,
						singleSelect:true,
						height:'auto',
						columns:[[
							{field:'budget_item_value',title:'名称',width:210,
								 editor:{   
						                type:'combobox',   
						                options:{   
						                valueField:'value',   
						                textField:'item',   
						                data:gdqx_data,   
						                required:true ,
						                 onSelect:function(record){
						                	//alert(record.value);
						                	var totalAmount=0.0;
						                	//获得公式
						                	if(record.item=="管道疏通大于50m"){
						                		totalAmount = 10000;
						                	}else{
						                		totalAmount = record.value;
						                	}
						                	//alert(totalAmount);
						                	var row = $('#ddv-'+index).datagrid('getSelected');
						                	var rowindex = $('#ddv-'+index).datagrid('getRowIndex', row);          	
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{item_unit_price:record.value}}); 
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{totalAmount:totalAmount}}); 
						                } 
						                 }   
						           }   
							
							
							
							},
							{field:'item_unit',title:'单位',width:100},
							{field:'budget_do_quantities',title:'工程量',width:100,editor:'numberbox'},
							{field:'item_unit_price',title:'单价',width:100},
							{field:'totalAmount',title:'合价',width:100}
						
						]],
						toolbar:[{
							text:'保存',
							iconCls:'icon-save',
							handler:function(){
		
								var rows = $('#ddv-'+index).datagrid('getRows');
								var total =0;
								for(i=0;i<rows.length;i++){
									var tmp = rows[i].totalAmount;
									//alert(tmp);
									if(!isNaN(tmp)){
										total = total+parseFloat(tmp);
									}
								}
								//alert(totalPrice);
								$('#dg').datagrid('updateRow',{index:index,row:{totalAmount:total}});
								
							}
					
						}],
						  
						   onClickRow:function(rowIndex){
								if (lastIndex != rowIndex){
									$('#ddv-'+index).datagrid('endEdit', lastIndex);
									$('#ddv-'+index).datagrid('beginEdit', rowIndex);
								}
								lastIndex = rowIndex;
							},
							 
						onResize:function(){
							$('#dg').datagrid('fixDetailRowHeight',index);
						},
						onLoadSuccess:function(){
							setTimeout(function(){
								$('#dg').datagrid('fixDetailRowHeight',index);
							},0);
						}
					});
			
			};
			
			
			*/
			// 化粪池维护（按时间段）

			if(index==1){
				//选择
				var parent_item_id = 6;
				
				var hfcwh_date_Datas = 
					[{item:'0年',value:'0-0'},
					 {item:'1年',value:'1-0.9'},
					 {item:'2年',value:'2-0.85'},
					 {item:'3年',value:'3-0.80'}
					];
			
				
				 $('#ddv-'+index).datagrid({
					 	url:baseURL+'/loadBudgetItem_gdstAction!loadhfcwhDateBudgetDoneItem.do',
						queryParams:{
							parent_item_id:parent_item_id,
							project_workinfo_id:project_workinfo_id
							},	
						fitColumns:true,
						singleSelect:true,
						height:'auto',
						columns:[[
							{field:'budget_item_name',title:'名称',width:210,
								formatter:function(value,rowData,rowIndex){
									for(var i=0; i<hfcwh_date_data.length; i++){
										if (hfcwh_date_data[i].budget_item_name == value) return hfcwh_date_data[i].budget_item_value;
									}
									return value;
								},
								 editor:{   
						                type:'combobox',   
						                options:{   
						                valueField:'budget_item_name',   
								        textField:'budget_item_value',     
						                data:hfcwh_date_data,   
						                required:true ,
						                 onSelect:function(record){
						                	//alert(record.budget_item_value);
						                	var value = record.budget_item_value;
						                	var y = value.substr(value.indexOf('-')+1);
						                	var para  = record.item_formula;
						                	
						                	//alert(y+"----"+para);
						                	
						                	var selectRow1 = $('#dg').datagrid('selectRow',0);
											var row1 = $('#dg').datagrid('getSelected');
											var value1= row1.totalAmount;
											
											
										   	var selectRow2 = $('#dg').datagrid('selectRow',1);
											var row2 = $('#dg').datagrid('getSelected');
											var value2= row2.totalAmount;
											
										   	var selectRow3 = $('#dg').datagrid('selectRow',2);
											var row3 = $('#dg').datagrid('getSelected');
											var value3= row3.totalAmount;
											
											var t_value = parseFloat(value1)+parseFloat(value2)+parseFloat(value3);
											//alert (t_value*para*y); 
											var item_unit_price = Math.round(t_value*para);
											
											var totalAmount = Math.round(t_value*para*y);
						                	
						                	
						              
						                	var row = $('#ddv-'+index).datagrid('getSelected');
						                	var rowindex = $('#ddv-'+index).datagrid('getRowIndex', row);          	
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{budget_do_quantities:y}}); 
						                	
						                	
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{item_id:record.item_id}});
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{budget_item:record.budget_item_name}}); 
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{budget_item_value:record.budget_item_value}}); 
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{item_unit_price:item_unit_price.toString()}}); 
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{totalAmount:totalAmount}}); 
						                	
						                	
						                } 
						                 }   
						           }   
							
							
							
							},
							{field:'item_id',title:'id',width:10,hidden:true},
							{field:'budget_item',title:'budget_item',hidden:true},
							{field:'budget_item_value',title:'budget_item_value',width:10,hidden:true},
							{field:'item_unit',title:'单位',width:100},
							{field:'budget_do_quantities',title:'工程量',width:100,editor:'numberbox'},
							{field:'item_unit_price',title:'单价',width:100},
							{field:'totalAmount',title:'合价',width:100}
						
						]],
						toolbar:[{
							text:'确定',
							iconCls:'icon-save',
							handler:function(){
		
								var rows = $('#ddv-'+index).datagrid('getRows');
								var total =0;
								for(i=0;i<rows.length;i++){
									var tmp = rows[i].totalAmount;
									//alert(tmp);
									if(!isNaN(tmp)){
										total = total+parseFloat(tmp);
									}
								}
								//alert(totalPrice);
								$('#dg').datagrid('updateRow',{index:index,row:{totalAmount:total}});
								
								//保存到后台
								hfcwh_date_save(rows);
								
							}
					
						}],
						  
						   onClickRow:function(rowIndex){
								if (lastIndex != rowIndex){
									$('#ddv-'+index).datagrid('endEdit', lastIndex);
									$('#ddv-'+index).datagrid('beginEdit', rowIndex);
								}
								lastIndex = rowIndex;
							},
							 
						onResize:function(){
							$('#dg').datagrid('fixDetailRowHeight',index);
						},
						onLoadSuccess:function(){
							setTimeout(function(){
								$('#dg').datagrid('fixDetailRowHeight',index);
							},0);
						}
					});
			
				
			};
			
			
			
			
			// 化粪池维护（按固定次数）

			if(index==2){
				
				var parent_item_id = 7;
				
				//选择
				var hfcwh_date_times = 
					[{item:'0次',value:'0-0'},
					 {item:'1次',value:'1-0.9'},
					 {item:'2次',value:'2-0.85'},
					 {item:'3次',value:'3-0.80'}
					];
			    
				 $('#ddv-'+index).datagrid({
					    url:baseURL+'/loadBudgetItem_gdstAction!loadhfcwhtimesBudgetDoneItem.do',
						queryParams:{
							parent_item_id:parent_item_id,
							project_workinfo_id:project_workinfo_id
							},		
						fitColumns:true,
						singleSelect:true,
						height:'auto',
						columns:[[
							{field:'budget_item_name',title:'名称',width:210,
								formatter:function(value,rowData,rowIndex){
									for(var i=0; i<hfcwh_times_data.length; i++){
										if (hfcwh_times_data[i].budget_item_name == value) return hfcwh_times_data[i].budget_item_value;
									}
									return value;
								},
								 editor:{   
						                type:'combobox',   
						                options:{   
						                valueField:'budget_item_name',   
										textField:'budget_item_value',    
						                data:hfcwh_times_data,   
						                required:true ,
						                 onSelect:function(record){
						                	 
						                	 	var value = record.budget_item_value;
							                	var y = value.substr(value.indexOf('-')+1);
							                	var para  = record.item_formula;
							                	
							                	//alert(y+"----"+para);
							                	
							                	var selectRow1 = $('#dg').datagrid('selectRow',0);
												var row1 = $('#dg').datagrid('getSelected');
												var value1= row1.totalAmount;
												
												
											   	var selectRow2 = $('#dg').datagrid('selectRow',1);
												var row2 = $('#dg').datagrid('getSelected');
												var value2= row2.totalAmount;
												
											   	var selectRow3 = $('#dg').datagrid('selectRow',2);
												var row3 = $('#dg').datagrid('getSelected');
												var value3= row3.totalAmount;
												
												var t_value = parseFloat(value1)+parseFloat(value2)+parseFloat(value3);
												//alert (t_value*para*y); 
												var item_unit_price = Math.round(t_value*para);
												
							                	
												var totalAmount = Math.round(t_value*para*y);
						                	
						                	//alert(totalAmount);
						                	var row = $('#ddv-'+index).datagrid('getSelected');
						                	var rowindex = $('#ddv-'+index).datagrid('getRowIndex', row);          	
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{budget_do_quantities:y}}); 
						                	
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{item_id:record.item_id}});
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{budget_item:record.budget_item_name}}); 
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{budget_item_value:record.budget_item_value}}); 
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{item_unit_price:item_unit_price.toString()}}); 
						                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{totalAmount:totalAmount}}); 
						                	
						                	
						                } 
						                 }   
						           }   
							
							
							
							},
							{field:'item_id',title:'id',width:10,hidden:true},
							{field:'budget_item',title:'budget_item',hidden:true},
							{field:'budget_item_value',title:'budget_item_value',width:10,hidden:true},
							{field:'item_unit',title:'单位',width:100},
							{field:'budget_do_quantities',title:'工程量',width:100,editor:'numberbox'},
							{field:'item_unit_price',title:'单价',width:100},
							{field:'totalAmount',title:'合价',width:100}
						
						]],
						toolbar:[{
							text:'确定',
							iconCls:'icon-save',
							handler:function(){
		
								var rows = $('#ddv-'+index).datagrid('getRows');
								var total =0;
								for(i=0;i<rows.length;i++){
									var tmp = rows[i].totalAmount;
									//alert(tmp);
									if(!isNaN(tmp)){
										total = total+parseFloat(tmp);
									}
								}
								//alert(totalPrice);
								$('#dg').datagrid('updateRow',{index:index,row:{totalAmount:total}});
								//保存到后台
								hfcwh_times_save(rows);
							}
					
						}],
						  
						   onClickRow:function(rowIndex){
								if (lastIndex != rowIndex){
									$('#ddv-'+index).datagrid('endEdit', lastIndex);
									$('#ddv-'+index).datagrid('beginEdit', rowIndex);
								}
								lastIndex = rowIndex;
							},
							 
						onResize:function(){
							$('#dg').datagrid('fixDetailRowHeight',index);
						},
						onLoadSuccess:function(){
							setTimeout(function(){
								$('#dg').datagrid('fixDetailRowHeight',index);
							},0);
						}
					});
			};
			
			
			
			if(index==3){
				var lastIndex;
				$('#ddv-'+index).datagrid({
					url:'subitem.json',
					fitColumns:true,
					singleSelect:true,
					height:'auto',
					columns:[[
						{field:'id',title:'序号',width:10},
						{field:'b_item',title:'预算项',width:10,
							
							 formatter:function(value){   
								                 for(var i=0; i<products.length; i++){   
								                     if (products[i].value == value) return products[i].b_item;   
								                 }   
								                 return value;   
								            },  
								 
							 editor:{   
								                type:'combobox',   
								                options:{   
								                valueField:'value',   
								                textField:'b_item',   
								                data:products,   
								                required:true ,
								                 onSelect:function(record){
								                	//alert(record.value);
								                	var row = $('#ddv-'+index).datagrid('getSelected');
								                	var rowindex = $('#ddv-'+index).datagrid('getRowIndex', row);
								                	
								                	$('#ddv-'+index).datagrid('updateRow',{index:rowindex,row:{price:record.value}}); 
								                    
								                } 
								                 }   
								           }   
	
						
						},
						{field:'price',title:'金额',width:10}
					
					]],
					
					toolbar:[{
						text:'确定',
						iconCls:'icon-save',
						handler:function(){
							var rows = $('#ddv-'+index).datagrid('getRows');
							var totalPrice =0;
							for(i=0;i<rows.length;i++){
								var tmp_price = rows[i].price;
								var price = tmp_price.substr(tmp_price.indexOf('-')+1);
								var item = tmp_price.substr(0,tmp_price.indexOf('-'));
				
								if(item=='水电费乙供'){
									$('#dg').datagrid('selectRow',0);
									var row = $('#dg').datagrid('getSelected');
									price = Math.round(0.04*parseFloat(row.totalAmount));
								}
								if(!isNaN(price)){
									totalPrice = totalPrice+parseFloat(price);
								}
							}
							//alert(totalPrice);
							$('#dg').datagrid('updateRow',{index:index,row:{totalAmount:totalPrice}});
							
						}
				
					}],
					
					
					onResize:function(){
						$('#dg').datagrid('fixDetailRowHeight',index);
					},
					onLoadSuccess:function(){
						setTimeout(function(){
							$('#dg').datagrid('fixDetailRowHeight',index);
							},0);
					},
					onClickRow:function(rowIndex){
						if (lastIndex != rowIndex){
							$('#ddv-'+index).datagrid('endEdit', lastIndex);
							$('#ddv-'+index).datagrid('beginEdit', rowIndex);
						}
						lastIndex = rowIndex;
					},
					  onAfterEdit:function(subindex,subrow){   
						  subrow.editing = false;   
						  subrow.price = subrow.b_item;
						  $('#ddv-'+index).datagrid('refreshRow',subindex);   
					 }
				});
				};
			
				
			$('#dg').datagrid('fixDetailRowHeight',index);
		}
	});
});
}); 